'''
1.准备好所有的top250地址
2.遍历所有地址
3.代码优化
'''
import requests
from lxml import etree

num = 1
def getData(url):
    global num
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    page = response.text
    html = etree.HTML(page)
    data_list = html.xpath("//ol/li")
    for li in data_list:
        title = li.xpath('.//span[@class="title"][1]/text()')
        source = li.xpath('.//span[@class="rating_num"][1]/text()')
        sub = li.xpath('.//span[@class="inq"][1]/text()')
        evaluate = li.xpath('.//div[@class="star"]/span[last()]/text()')
        title0 = title[0] if len(title) > 0 else ''
        source0 = source[0] if len(source) > 0 else ''
        sub0 = sub[0] if len(sub) > 0 else ''
        evaluate0 = evaluate[0] if len(evaluate) > 0 else ''
        print(f'{num}.电影名称:{title0},评分:{source0},类型:{sub0},评价人数:{evaluate0}')
        num = num+1



if __name__== '__main__':
    lst =[i*25 for i in range(10)]
    for i in lst:
        getData(f'https://movie.douban.com/top250?start={i}&filter=')